﻿#include "WeaponUpLayer.h"
#include "ui/WeaponItem.h"
#include "hero/Hero.h"
#include "utils/MyStringUtils.h"
#include <string>
using namespace std;
using namespace cocos2d;
using namespace cocos2d::ui;//UI命名空间
#define HERO 50
WeaponUpLayer::WeaponUpLayer() {

}

WeaponUpLayer* WeaponUpLayer::create() {
	WeaponUpLayer *ret = new (std::nothrow)WeaponUpLayer();
	if (ret && ret->init()) {
		ret->autorelease();
		return ret;
	} else {
		CC_SAFE_DELETE(ret);
		return nullptr;
	}
}

bool WeaponUpLayer::init() {
	if (!Layer::init()) {
		return false;
	}
	initCSBFile();
	initUserDefault();
	addScrollView();
	return true;
}

void WeaponUpLayer::initCSBFile() {
	size = Director::getInstance()->getVisibleSize();
	rootNode = CSLoader::createNode("scenes/start/layer/WeaponUpLayer.csb");
	this->addChild(rootNode);

	strength_text = dynamic_cast<Text*>(rootNode->getChildByName("strength_text"));
	huoli_text = dynamic_cast<Text*>(rootNode->getChildByName("huoli_text"));

	strength_value = dynamic_cast<TextAtlas*>(rootNode->getChildByName("strength_value"));
	huoli_value = dynamic_cast<TextAtlas*>(rootNode->getChildByName("huoli_value"));

	strength_UpCoinNum = dynamic_cast<Text*>(rootNode->getChildByName("strengthUpCoinNum"));
	huoli_UpCoinNum = dynamic_cast<Text*>(rootNode->getChildByName("huoliUpCoinNum"));

	strengthUpbtn = dynamic_cast<Button*>(rootNode->getChildByName("strengthUpbtn"));
	strengthUpbtn->addClickEventListener([this](Ref*) {
		//射速升级逻辑
		_eventDispatcher->dispatchCustomEvent("onWeaponStrengthUp", this);
		addStrengthCoin();
		addStrengthLevelAndValue();
	});

	huoliUpbtn = dynamic_cast<Button*>(rootNode->getChildByName("huoliUpbtn"));
	huoliUpbtn->addClickEventListener([this](Ref*) {
		//火力升级逻辑
		_eventDispatcher->dispatchCustomEvent("onWeaponHuoliUp", this);
		addHuoliCoin();
		addHuoliLevelAndValue();
	});
}

void WeaponUpLayer::onExit() {
	Layer::onExit();
	UserDefault::getInstance()->setIntegerForKey("strength_text", strengthText);
	UserDefault::getInstance()->setIntegerForKey("weaponHuoli_text", huoliText);

	UserDefault::getInstance()->setIntegerForKey("strength_value", strengthValue);
	UserDefault::getInstance()->setIntegerForKey("weaponHuoli_value", huoliValue);

	UserDefault::getInstance()->setIntegerForKey("strengthUpCoinNum", strengthUpCoinNum);
	UserDefault::getInstance()->setIntegerForKey("weaponHuoliUpCoinNum", huoliUpCoinNum);
}

/*
	加载用户数据
*/
void WeaponUpLayer::initUserDefault() {
	char filename[10];
	strengthText = UserDefault::getInstance()->getIntegerForKey("strength_text", 1);
	huoliText = UserDefault::getInstance()->getIntegerForKey("weaponHuoli_text", 1);
	sprintf_s(filename, "[Lv %d]", strengthText);
	strength_text->setString(filename);
	sprintf_s(filename, "[Lv %d]", huoliText);
	huoli_text->setString(filename);

	strengthValue = UserDefault::getInstance()->getIntegerForKey("strength_value", 10);
	huoliValue = UserDefault::getInstance()->getIntegerForKey("weaponHuoli_value", 10);
	strength_value->setString(to_string(strengthValue));
	huoli_value->setString(to_string(huoliValue));

	strengthUpCoinNum = UserDefault::getInstance()->getIntegerForKey("strengthUpCoinNum", 50);
	huoliUpCoinNum = UserDefault::getInstance()->getIntegerForKey("weaponHuoliUpCoinNum", 50);
	strength_UpCoinNum->setString(MyStringUtils::getInstance()->coinToString(strengthUpCoinNum));
	huoli_UpCoinNum->setString(MyStringUtils::getInstance()->coinToString(huoliUpCoinNum));
}

void WeaponUpLayer::addStrengthCoin() {
	strengthUpCoinNum += 100;
	strength_UpCoinNum->setString(MyStringUtils::getInstance()->coinToString(strengthUpCoinNum));
}

void WeaponUpLayer::addHuoliCoin() {
	huoliUpCoinNum += 100;
	huoli_UpCoinNum->setString(MyStringUtils::getInstance()->MyStringUtils::getInstance()->coinToString(huoliUpCoinNum));
}

void WeaponUpLayer::addStrengthLevelAndValue() {
	char filename[10];
	strengthText += 1;
	sprintf_s(filename, "[Lv %d]", strengthText);
	strength_text->setString(filename);

	strengthValue += 1;
	strength_value->setString(MyStringUtils::getInstance()->coinToString(strengthValue));
}

void WeaponUpLayer::addHuoliLevelAndValue() {
	char filename[10];
	huoliText += 1;
	sprintf_s(filename, "[Lv %d]", huoliText);
	huoli_text->setString(filename);

	huoliValue += 1;
	huoli_value->setString(MyStringUtils::getInstance()->coinToString(huoliValue));
}

void WeaponUpLayer::addScrollView() {
	//获取ScrollView子节点
	ScrollView* scrollView = dynamic_cast<ScrollView*>(rootNode->getChildByName("scrollView"));
	int select = UserDefault::getInstance()->getIntegerForKey("selected", 0);

	//设置滚动区域的innerSize
	scrollView->setInnerContainerSize(Size(104 * 5, 90));
	std::vector<WeaponItem*> items;
	//添加子控件，子控件必须作为scrollView的子节点
	for (int i = 0; i < 5; i++) {
		WeaponItem* item = WeaponItem::create(i);
		item->setPosition(Vec2(i * 104, 0));
		scrollView->addChild(item);
		items.push_back(item);
		//根据本地选择
		if (i == select) {
			item->setSelected(true);
		}
	}

	for each (WeaponItem* item in items) {
		//给item添加监听事件
		item->addClickEventListener([this, item, items](Ref*) {
			//AudioUtil::getInstence()->buttonClickSound();
			for (WeaponItem* temp : items) {
				temp->setSelected(false);
			}
			//设置被点击选项选中
			item->setSelected(true);
			int select = UserDefault::getInstance()->getIntegerForKey("selected", 0);
			heroChange(select);
		});
	}
}

void WeaponUpLayer::heroChange(int selected) {
	hero = Hero::getInstance();
	MoveTo* mt = MoveTo::create(0.2f, Vec2(650, hero->getPositionY()));
	int heroPosY = hero->getPositionY();
	CallFunc* callFunc = CallFunc::create([this, heroPosY]() {
		hero->collectionHero();
		hero = nullptr;
		hero = Hero::getInstance();
		hero->setPosition(Vec2(650, heroPosY));
		_parent->addChild(hero);

		MoveTo* mt = MoveTo::create(0.2f, Vec2(size.width / 2, heroPosY));
		hero->runAction(mt);
	});
	Sequence* seqAct = Sequence::create(mt, callFunc, nullptr);
	hero->runAction(seqAct);
}